package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "C端-地址簿接口")
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;

    /**
     * @description: 新增地址
     * @author: tanbobin
     * @date: 2024/6/6 11:21
     * @param:
     * @param addressBook
     * @return:
     * @return com.sky.result.Result
     **/
    @ApiOperation("新增地址")
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }

    @ApiOperation("查询当前用户所有地址列表")
    @GetMapping("/list")
    public Result<List<AddressBook>> queryList(){
        List<AddressBook> addressBookList = addressBookService.queryList();
        return Result.success(addressBookList);
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook){
        addressBookService.setDefaultAddress(addressBook);
        return Result.success();
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> queryDefaultAddress(){
        AddressBook addressBook = addressBookService.queryDefault();
        if (ObjectUtils.isEmpty(addressBook)){
            return Result.error("没有查询到默认地址");
        }
        return Result.success(addressBook);
    }

    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> queryAddressById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.queryAddressById(id);
        return Result.success(addressBook);
    }

    @ApiOperation("根据id修改地址")
    @PutMapping
    public Result updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result deleteAddress(Long id){
        addressBookService.deleteAddress(id);
        return Result.success();
    }
}
